package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//冒泡排序
public class BubbleSort {

    public static void main(String[] args) {
//        int arr[] = {1,-1,2,2,9};

        //自定义一个80000个数据的数组
        int arr[] = new int[8];
        for (int i=0;i<8;i++){
            arr[i] = (int) (Math.random() * 8000000);
        }
        System.out.println(Arrays.toString(arr));
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
        String format = simpleDateFormat.format(date1);
        System.out.println("遍历前的时间"+format);
        bubble(arr);
        Date date2 = new Date();
        String format1 = simpleDateFormat.format(date2);
        System.out.println(Arrays.toString(arr));
        System.out.println("遍历后的时间"+format1);
    }



    public static void bubble(int arr[]) {

        boolean flag = false;
        int temp = 0;
        for (int j = 0; j < arr.length; j++) {
            for (int i = 0; i < arr.length-1-j; i++) {
                if (arr[i] > arr[i + 1]) {
                    flag = true;
                    temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
            }

            if (!flag){
                break;
            }else {
                flag = false;
            }
        }
    }
}
